package com.situ.servlet;

import com.alibaba.fastjson.JSON;
import com.situ.annotation.Param;
import com.situ.dto.PageEntity;
import com.situ.entity.Ward;
import com.situ.service.IWardService;
import com.situ.service.impl.WardServiceImpl;
import com.situ.util.BaseServlet;
import com.situ.vo.ResultDataVO;
import sun.jvm.hotspot.debugger.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ward")
public class WardServlet extends BaseServlet {
    private static final long serialVersionUID = 2128198831936717412L;

    IWardService wardService = new WardServiceImpl();

    public void list(@Param("wardno") String wardno, PageEntity page, HttpServletRequest request, HttpServletResponse response) {

        List<Ward> list = wardService.selectList(wardno, page);
        Integer sum = wardService.countList(wardno);

        ResultDataVO vo = new ResultDataVO();
        vo.setCount(sum);
        vo.setData(list);
        try {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(JSON.toJSONString(vo));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void save(Ward ward, HttpServletResponse response) throws IOException {
        if (ward.getId() == null) {
            wardService.insert(ward);
        } else {
            wardService.update(ward);
        }
        response.sendRedirect("/ward/list.jsp");
    }

    public void edit(@Param("id") Integer id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Ward ward = wardService.selectById(id);
        request.setAttribute("ward", ward);
        request.getRequestDispatcher("/ward/add.jsp").forward(request, response);

    }

    public void del(@Param("id")Integer id){
        wardService.deleteById(id);
    }

}
